package service

import (
	"context"
	"rpc_video/bassic/config"
	__ "rpc_video/bassic/proto"
	"rpc_video/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedVideoServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ManageVideo(_ context.Context, in *__.ManageVideoReq) (*__.ManageVideoResp, error) {
	video := model.Video{
		Title:       in.Title,
		Description: in.Description,
		VideoUrl:    in.VideoUrl,
		CoverUrl:    in.CoverUrl,
		Status:      in.Status,
		UserId:      in.UserId,
		CategoryId:  in.CategoryId,
		MemberId:    in.MemberId,
	}
	tx := config.DB.Begin()
	tx.Create(&video)
	tx.Rollback()
	tx.Commit()
	return &__.ManageVideoResp{
		Code: 200,
		Msg:  "OK",
	}, nil
}

func (s *Server) ManageRecord(_ context.Context, in *__.ManageRecordReq) (*__.ManageRecordResp, error) {
	video := model.Video{
		MemberId: in.MemberId,
	}
	if err := config.DB.Find(&video).Error; err != nil {
		return &__.ManageRecordResp{
			Code: 500,
			Msg:  err.Error(),
		}, err
	}
	return &__.ManageRecordResp{
		Code: 200,
		Msg:  "OK",
	}, nil
}
